package com.atguigu.yygh.hosp.contoller;

import com.atguigu.yygh.hosp.common.base.BaseController;
import com.atguigu.yygh.hosp.common.result.Result;
import com.atguigu.yygh.hosp.common.utils.MD5;
import com.atguigu.yygh.hosp.model.hosp.HospitalSet;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Random;

/**
 * @version 1.0
 * @Author: 许啸林
 * @Date: 2021/6/30 12:44
 * 医院设置控制类
 */

@Api("医院设置管理")
@RestController
@CrossOrigin
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController { //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping()
    public Result removeHospSet(@RequestBody List<Long> ids) {
        if(ids.size() <= 0) {
            return  Result.fail("请选择删除列");
        }
        boolean flag = false;
        if(ids.size() > 1) {
          flag = hospitalSetService.removeByIds(ids);
        }else{
          flag = hospitalSetService.removeById(ids.get(0));
        }
        return BaseController.getResult(flag);
    }

    @ApiOperation(value = "医院设置分页查询")
    @PostMapping("findPageHospSet")
    public Result findPageHospSet(@RequestBody (required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = BaseController.getPage();
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if(hospitalSetQueryVo != null) {
            // 医院名称
            String hosname = hospitalSetQueryVo.getHosname();
            // 医院编号
            String hoscode = hospitalSetQueryVo.getHoscode();
            if(!StringUtils.isEmpty(hosname)) {
                wrapper.like("hosname",hospitalSetQueryVo.getHosname());
            }
            if(!StringUtils.isEmpty(hoscode)) {
                wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
            }
        }
        //调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        //返回结果
        return Result.ok(pageHospitalSet);
    }

    @ApiOperation("保存设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@ApiParam("医院设置对象") @RequestBody HospitalSet hospitalSet) {
        //设置状态 0禁用 1启用
        hospitalSet.setStatus(0);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //调用service
        boolean flag = hospitalSetService.save(hospitalSet);
        return BaseController.getResult(flag);
    }

    @ApiOperation("根据主键获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    @ApiOperation("修改医院设置")
    @PostMapping("editHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return BaseController.getResult(flag);
    }

    @ApiOperation("锁定解锁医院设置")
    @PutMapping("lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable Long id, Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if(hospitalSet == null) {
            return  Result.fail("医院设置信息不存在");
        }
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return BaseController.getResult(flag);
    }
}
